home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 114 / PC Guia 114.iso / Software / Utils / The Gimp 2.2.1 / gimp-help-2-0.6-setup.exe / {app} / share / gimp / 2.0 / help / en / ch02s10s02.html < prev    next >
Encoding:
Extensible Markup Language  |  2004-12-19  |  11.1 KB  |  288 lines

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.     <title>10.2.┬áVariables And Functions</title>
  7.     <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
  8.     <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
  9.     <meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
  10.     <link rel="start" href="index.html" title="GIMP User Manual" />
  11.     <link rel="up" href="ch02s10.html" title="10.┬áA Script-Fu Tutorial" />
  12.     <link rel="prev" href="ch02s10.html" title="10.┬áA Script-Fu Tutorial" />
  13.     <link rel="next" href="ch02s10s03.html" title="10.3.┬áLists, Lists And More Lists" />
  14.   </head>
  15.   <body>
  16.     <div xmlns="" class="navheader">
  17.       <table width="100%" summary="Navigation header">
  18.         <tr>
  19.           <th colspan="3" align="center" id="chaptername">10.┬áA Script-Fu Tutorial</th>
  20.         </tr>
  21.         <tr>
  22.           <td width="20%" align="left"><a accesskey="p" href="ch02s10.html">Prev</a>┬á</td>
  23.           <th width="60%" align="center" id="sectionname">10.2.┬áVariables And Functions</th>
  24.           <td width="20%" align="right">┬á<a accesskey="n" href="ch02s10s03.html">Next</a></td>
  25.         </tr>
  26.       </table>
  27.       <hr />
  28.     </div>
  29.     <div class="sect2" lang="en" xml:lang="en">
  30.       <div class="titlepage">
  31.         <div>
  32.           <div>
  33.             <h3 class="title"><a id="id3429359"></a>10.2.┬áVariables And Functions</h3>
  34.           </div>
  35.         </div>
  36.       </div>
  37.       <p>
  38.       Now that we know that every Scheme statement is enclosed in
  39.       parentheses, and that the function name/operator is listed first,
  40.       we need to know how to create and use variables, and how to create
  41.       and use functions. We'll start with the variables. 
  42.     </p>
  43.       <div class="simplesect" lang="en" xml:lang="en">
  44.         <div class="titlepage">
  45.           <div>
  46.             <div>
  47.               <h4 class="title"><a id="id3429375"></a>Declaring Variables</h4>
  48.             </div>
  49.           </div>
  50.         </div>
  51.         <p>
  52.         Although there are a couple of different methods for declaring
  53.         variables, the preferred method is to use the let*
  54.         construct. If you're familiar with other programming
  55.         languages, this construct is equivalent to defining a list of
  56.         local variables and a scope in which they're active. As an
  57.         example, to declare two variables, a and b, initialized to 1
  58.         and 2, respectively, you'd write: 
  59.       </p>
  60.         <pre class="programlisting">
  61.          (let*  (
  62.                    (a 1)
  63.                    (b 2)
  64.                 )
  65.                 (+ a b)
  66.          )
  67.       </pre>
  68.         <p>
  69.         or, as one line:
  70.       </p>
  71.         <pre class="programlisting">
  72.         (let* ( (a 1) (b 2) ) (+ a b) )
  73.       </pre>
  74.         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
  75.           <table border="0" summary="Note">
  76.             <tr>
  77.               <td rowspan="2" align="center" valign="top" width="25">
  78.                 <img alt="[Note]" src="../images/note.png" />
  79.               </td>
  80.               <th align="left">Note</th>
  81.             </tr>
  82.             <tr>
  83.               <td colspan="2" align="left" valign="top">
  84.                 <p>
  85.       You'll have to put all of this on one line if you're using the
  86.       console window. In general, however, you'll want to adopt a
  87.       similar practice of indentation to help make your scripts more
  88.       readable. We'll talk a bit more about this in the section on
  89.       White Space. 
  90.     </p>
  91.               </td>
  92.             </tr>
  93.           </table>
  94.         </div>
  95.         <p>
  96.         This declares two local variables, a and b, initializes them,
  97.         then prints the sum of the two variables. 
  98.       </p>
  99.       </div>
  100.       <div class="simplesect" lang="en" xml:lang="en">
  101.         <div class="titlepage">
  102.           <div>
  103.             <div>
  104.               <h4 class="title"><a id="id3429424"></a>What Is A Local Variable?</h4>
  105.             </div>
  106.           </div>
  107.         </div>
  108.         <p>
  109.         You'll notice that we wrote the summation <tt class="code">(+ a b)</tt>
  110.         within the parens of the <tt class="code">let*</tt> expression, not after it. 
  111.       </p>
  112.         <p>
  113.         This is because the <tt class="code">let*</tt> statement defines an
  114.         area in your script in which the declared variables are
  115.         usable; if you type the (+ a b) statement after the (let* ...)
  116.         statement, you'll get an error, because the declared variables
  117.         are only valid within the context of the <tt class="code">let*</tt> statement; they
  118.         are what programmers call local variables. 
  119.       </p>
  120.       </div>
  121.       <div class="simplesect" lang="en" xml:lang="en">
  122.         <div class="titlepage">
  123.           <div>
  124.             <div>
  125.               <h4 class="title"><a id="id3429462"></a>The General Syntax Of <tt class="code">let*</tt></h4>
  126.             </div>
  127.           </div>
  128.         </div>
  129.         <p>
  130.         The general form of a <tt class="code">let*</tt> statement is:
  131.       </p>
  132.         <pre class="programlisting">
  133.         (let* ( <i class="replaceable"><tt>variables</tt></i> ) <i class="replaceable"><tt>expressions</tt></i> )
  134.       </pre>
  135.         <p>
  136.         where variables are declared within parens, e.g., (a 2), and
  137.         expressions are any valid Scheme expressions. Remember that
  138.         the variables declared here are only valid within the
  139.         <tt class="code">let*</tt> statement -- they're local variables. 
  140.       </p>
  141.       </div>
  142.       <div class="simplesect" lang="en" xml:lang="en">
  143.         <div class="titlepage">
  144.           <div>
  145.             <div>
  146.               <h4 class="title"><a id="id3429506"></a>White Space</h4>
  147.             </div>
  148.           </div>
  149.         </div>
  150.         <p>
  151.         Previously, we mentioned the fact that you'll probably want to
  152.         use indentation to help clarify and organize your
  153.         scripts. This is a good policy to adopt, and is not a problem
  154.         in Scheme -- white space is ignored by the Scheme interpreter,
  155.         and can thus be liberally applied to help clarify and organize
  156.         the code within a script. However, if you're working in
  157.         Script-Fu's Console window, you'll have to enter an entire
  158.         expression on one line; that is, everything between the
  159.         opening and closing parens of an expression must come on one
  160.         line in the Script-Fu Console window. 
  161.       </p>
  162.       </div>
  163.       <div class="simplesect" lang="en" xml:lang="en">
  164.         <div class="titlepage">
  165.           <div>
  166.             <div>
  167.               <h4 class="title"><a id="id3429528"></a>Assigning A New Value To A Variable</h4>
  168.             </div>
  169.           </div>
  170.         </div>
  171.         <p>
  172.         Once you've initialized a variable, you might need to change
  173.         its value later on in the script. Use the set! statement to
  174.         change the variable's value: 
  175.       </p>
  176.         <pre class="programlisting">
  177.         (let* ( (theNum 10) ) (set! theNum (+ theNum \ theNum)) )
  178.       </pre>
  179.         <p>
  180.         Try to guess what the above statement will do, then go ahead
  181.         and enter it in the Script-Fu Console window. 
  182.       </p>
  183.         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
  184.           <table border="0" summary="Note">
  185.             <tr>
  186.               <td rowspan="2" align="center" valign="top" width="25">
  187.                 <img alt="[Note]" src="../images/note.png" />
  188.               </td>
  189.               <th align="left">Note</th>
  190.             </tr>
  191.             <tr>
  192.               <td colspan="2" align="left" valign="top">
  193.                 <p>
  194.       The "\" indicates that there is no line break. Ignore it (don't
  195.       type it in your Script-Fu console and don't hit Enter), just
  196.       continue with the next line. 
  197.     </p>
  198.               </td>
  199.             </tr>
  200.           </table>
  201.         </div>
  202.       </div>
  203.       <div class="simplesect" lang="en" xml:lang="en">
  204.         <div class="titlepage">
  205.           <div>
  206.             <div>
  207.               <h4 class="title"><a id="id3429561"></a>Functions</h4>
  208.             </div>
  209.           </div>
  210.         </div>
  211.         <p>
  212.         Now that you've got the hang of variables, let's get to work
  213.         with some functions. You declare a function with the following
  214.         syntax: 
  215.       </p>
  216.         <pre class="programlisting">
  217.         (define (<i class="replaceable"><tt>name</tt></i> <i class="replaceable"><tt>param-list</tt></i>) <i class="replaceable"><tt>expressions</tt></i>)
  218.       </pre>
  219.         <p>
  220.         where <i class="replaceable"><tt>name</tt></i> is the name assigned to
  221.         this function, <i class="replaceable"><tt>param-list</tt></i> is a
  222.         space-delimited list of parameter names, and
  223.         <i class="replaceable"><tt>expressions</tt></i> is a series of
  224.         expressions that the function executes when it's called. For
  225.         example:  
  226.       </p>
  227.         <pre class="programlisting">
  228.         (define (AddXY inX inY) (+ inX inY) )
  229.       </pre>
  230.         <p>
  231.         <tt class="varname">AddXY</tt> is the function's name and
  232.         <tt class="varname">inX</tt> and <tt class="varname">inY</tt> are the
  233.         variables. This function takes its two parameters and adds
  234.         them together. 
  235.       </p>
  236.         <p>
  237.         If you've programmed in other imperative languages (like
  238.         C/C++, Java, Pascal, etc.), you might notice that a couple of
  239.         things are absent in this function definition when compared to
  240.         other programming languages. 
  241.       </p>
  242.         <div class="itemizedlist">
  243.           <ul type="disc">
  244.             <li>
  245.               <p>
  246.           First, notice that the parameters don't have any "types"
  247.           (that is, we didn't declare them as strings, or integers,
  248.           etc.). Scheme is a type-less language. This is handy and
  249.           allows for quicker script writing. 
  250.         </p>
  251.             </li>
  252.             <li>
  253.               <p>
  254.             Second, notice that we don't need to worry about how to
  255.             "return" the result of our function -- the last statement is
  256.             the value "returned" when calling this function. Type the
  257.             function into the console, then try something like:
  258.           </p>
  259.               <pre class="programlisting">
  260.             (AddXY (AddXY 5 6) 4)
  261.           </pre>
  262.             </li>
  263.           </ul>
  264.         </div>
  265.       </div>
  266.     </div>
  267.     <div class="navfooter">
  268.       <hr />
  269.       <table width="100%" summary="Navigation footer">
  270.         <tr>
  271.           <td width="40%" align="left"><a accesskey="p" href="ch02s10.html">Prev</a>┬á</td>
  272.           <td width="20%" align="center">
  273.             <a accesskey="u" href="ch02s10.html">Up</a>
  274.           </td>
  275.           <td width="40%" align="right">┬á<a accesskey="n" href="ch02s10s03.html">Next</a></td>
  276.         </tr>
  277.         <tr>
  278.           <td width="40%" align="left" valign="top">10.┬áA Script-Fu Tutorial┬á</td>
  279.           <td width="20%" align="center">
  280.             <a accesskey="h" href="index.html">Home</a>
  281.           </td>
  282.           <td width="40%" align="right" valign="top">┬á10.3.┬áLists, Lists And More Lists</td>
  283.         </tr>
  284.       </table>
  285.     </div>
  286.   </body>
  287. </html>
  288.